﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace OnlineBusTicket
{
    public partial class UpdateTour : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.DataBind();
        }

        private bool CheckTourExist()//fixed
        {
            bool tatkt = false;
            string maso = txtBuslistId.Text;
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            con.Open();

            SqlDataAdapter da_kiemtra = new SqlDataAdapter("Select * from Buslist where BusListId='" + maso + "'", con);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);

            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            return tatkt;
        }
        protected void Button2_Click1(object sender, EventArgs e)//add
        {
            
                if (CheckTourExist())
                {
                    lblStatusBusListId.Text = ("BusId has been exist, re-input...");
                    txtBuslistId.Focus();
                }
                else
                {
                    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                    con.Open();
                    SqlCommand cmd = 
                        new SqlCommand("INSERT INTO BusList Values('"+ txtBuslistId.Text 
                                                  + "',"  + Convert.ToInt32(txtTotalseat.Text)
                                                  + ",'" + txtCategory.Text 
                                                  + "','" + txtDepart.Text 
                                                  + "','" + txtArrive.Text 
                                                  + "','" + txtStartDay.Text 
                                                  + "','" + txtEndDay.Text
                                                  + "','" + txtStartTime.Text 
                                                  + "'," + Convert.ToInt32(txtPrice.Text) + ")", con);
                     SqlCommand cmd2 = new SqlCommand("INSERT INTO TotalSeat VALUES ('"+txtBuslistId.Text+"',0)",con);
                    cmd.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();
                    con.Close();

                    Response.Redirect("Buslist.aspx");
                }
            
        }

        protected void Button1_Click(object sender, EventArgs e)//update
        {
            if (CheckTourExist())
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE BusList SET [TotalNumberOfSeats]='" + Convert.ToInt32(txtTotalseat.Text)
                                                                + "',[BusCategory]='" + txtCategory.Text
                                                                + "',[DeparturePlace]='" + txtDepart.Text
                                                                + "',[ArrivePlace]='" + txtArrive.Text
                                                                + "',[StartDate]='" + txtStartDay.Text
                                                                + "',[Time]='" + txtStartTime.Text
                                                                + "',[EndDate]='" + txtEndDay.Text
                                                                + "',[Price]='" + Convert.ToInt32(txtPrice.Text)
                                                                + "' where [BusListId]='" + txtBuslistId.Text + "'", con);

                cmd.ExecuteNonQuery();
                con.Close();

                Response.Redirect("Buslist.aspx");
                
            }
            else
            {
                txtBuslistId.Text = ("BusId has not exist...");
                txtBuslistId.Focus();
            }
        }

        
     
    }
}